package com.hotai.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotai.comment.dtos.ResponseResult;
import com.hotai.comment.pojo.Statistics;
import com.hotai.comment.pojo.UserStudent;
import com.hotai.mapper.StatisticsMapper;
import com.hotai.mapper.UserStudentMapper;
import com.hotai.service.StatisticsService;
import com.hotai.service.UserStudentSerivice;
import com.hotai.vo.tongji.Xuekernel;
import com.hotai.vo.tongji.XuekernelPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class StatisticsServiceImpl extends ServiceImpl<StatisticsMapper, Statistics> implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public ResponseResult XueTong(XuekernelPage xuekernelPage) {
        //自然日
        Xuekernel xuekernel = new Xuekernel();
        if (xuekernelPage.getStatus() == 1) {
            Statistics xuekerday = statisticsMapper.xuekerday(xuekernelPage);
            //昨天数据
            Date date = dayList(xuekernelPage.getTime(), 1);
            xuekernelPage.setTime(date);
            Statistics yesterday = statisticsMapper.xuekerday(xuekernelPage);
            //较上一日新增用户
            int addCountLast = (Integer.parseInt(xuekerday.getUserCount()) - Integer.parseInt(yesterday.getUserCount())) * 100 / Integer.parseInt(yesterday.getUserCount());
            //较上一日用户总数
            int userCountLast = (Integer.parseInt(xuekerday.getUserZcount()) - Integer.parseInt(yesterday.getUserZcount())) * 100 / Integer.parseInt(yesterday.getUserZcount());
            //较上一日设备数量
            int eqCountLast = (Integer.parseInt(xuekerday.getEqCount()) - Integer.parseInt(yesterday.getEqCount())) * 100 / Integer.parseInt(yesterday.getEqCount());
            //较上一日学习时长
            int zongLast = (Integer.parseInt(xuekerday.getXueTime()) - Integer.parseInt(yesterday.getXueTime())) * 100 / Integer.parseInt(yesterday.getXueTime());
            //较上一日学习次数
            int xcountLast = (Integer.parseInt(xuekerday.getXueCount()) - Integer.parseInt(yesterday.getXueCount())) * 100 / Integer.parseInt(yesterday.getXueCount());
            //较上一日学习完课率
            int avgCountLast = (Integer.parseInt(xuekerday.getAvgCourse()) - Integer.parseInt(yesterday.getAvgCourse())) * 100 / Integer.parseInt(yesterday.getAvgCourse());
            //新增用户
            xuekernel.setAddCount((Integer.parseInt(xuekerday.getUserCount())));
            xuekernel.setAddCountLast(addCountLast);
            //用户总数
            xuekernel.setUserCount((Integer.parseInt(xuekerday.getUserZcount())));
            xuekernel.setUserCountLast(userCountLast);
            //设备总数
            xuekernel.setEqCount((Integer.parseInt(xuekerday.getEqCount())));
            xuekernel.setEqCountLast(eqCountLast);
            //学习时长
            xuekernel.setZong((Integer.parseInt(xuekerday.getXueTime())));
            xuekernel.setZongLast(zongLast);
            //学习次数
            xuekernel.setXcount((Integer.parseInt(xuekerday.getXueCount())));
            xuekernel.setXcountLast(xcountLast);
            //平均完课率
            xuekernel.setAvgCount((Integer.parseInt(xuekerday.getAvgCourse())));
            xuekernel.setAvgCountLast(avgCountLast);
            return ResponseResult.okResult(xuekernel);
        }
        if (xuekernelPage.getStatus() == 2){
            Statistics xuekerweek = statisticsMapper.xuekerweek(xuekernelPage);
            //相差天数
            int i = differentDays(xuekernelPage.getMinTime(), xuekernelPage.getMaxTime());
           //上一周时间
            Date  mintime = dayList(xuekernelPage.getMinTime(), i);

            Date  maxtime = dayList(xuekernelPage.getMaxTime(), i);
            xuekernelPage.setMinTime(mintime);
            xuekernelPage.setMaxTime(maxtime);
            Statistics yesterweek= statisticsMapper.xuekerweek(xuekernelPage);
            //较上一周新增用户
            int addCountLast = (Integer.parseInt(xuekerweek.getUserCount()) - Integer.parseInt(yesterweek.getUserCount())) * 100 / Integer.parseInt(yesterweek.getUserCount());
            //较上一周用户总数
            int userCountLast = (Integer.parseInt(xuekerweek.getUserZcount()) - Integer.parseInt(yesterweek.getUserZcount())) * 100 / Integer.parseInt(yesterweek.getUserZcount());
            //较上一周设备数量
            int eqCountLast = (Integer.parseInt(xuekerweek.getEqCount()) - Integer.parseInt(yesterweek.getEqCount())) * 100 / Integer.parseInt(yesterweek.getEqCount());
            //较上一周学习时长
            int zongLast = (Integer.parseInt(xuekerweek.getXueTime()) - Integer.parseInt(yesterweek.getXueTime())) * 100 / Integer.parseInt(yesterweek.getXueTime());
            //较上一周学习次数
            int xcountLast = (Integer.parseInt(xuekerweek.getXueCount()) - Integer.parseInt(yesterweek.getXueCount())) * 100 / Integer.parseInt(yesterweek.getXueCount());
            //较上一周学习完课率
            int avgCountLast = (Integer.parseInt(xuekerweek.getAvgCourse()) - Integer.parseInt(yesterweek.getAvgCourse())) * 100 / Integer.parseInt(yesterweek.getAvgCourse());
            //新增用户
            xuekernel.setAddCount((Integer.parseInt(xuekerweek.getUserCount())));
            xuekernel.setAddCountLast(addCountLast);
            //用户总数
            xuekernel.setUserCount((Integer.parseInt(xuekerweek.getUserZcount())));
            xuekernel.setUserCountLast(userCountLast);
            //设备总数
            xuekernel.setEqCount((Integer.parseInt(xuekerweek.getEqCount())));
            xuekernel.setEqCountLast(eqCountLast);
            //学习时长
            xuekernel.setZong((Integer.parseInt(xuekerweek.getXueTime())));
            xuekernel.setZongLast(zongLast);
            //学习次数
            xuekernel.setXcount((Integer.parseInt(xuekerweek.getXueCount())));
            xuekernel.setXcountLast(xcountLast);
            //平均完课率
            xuekernel.setAvgCount((Integer.parseInt(xuekerweek.getAvgCourse())));
            xuekernel.setAvgCountLast(avgCountLast);
            return ResponseResult.okResult(xuekernel);
        }
        if (xuekernelPage.getStatus() == 3){
            Statistics xuekerweek = statisticsMapper.xuekerweek(xuekernelPage);
            //相差天数
            int i = differentDays(xuekernelPage.getMinTime(), xuekernelPage.getMaxTime());
            //上一周时间
            Date  mintime = dayList(xuekernelPage.getTime(), i);

            Date  maxtime = dayList(xuekernelPage.getMaxTime(), i);
            xuekernelPage.setMinTime(mintime);
            xuekernelPage.setMaxTime(maxtime);
            Statistics yesterweek= statisticsMapper.xuekerweek(xuekernelPage);
            //较上一周新增用户
            int addCountLast = (Integer.parseInt(xuekerweek.getUserCount()) - Integer.parseInt(yesterweek.getUserCount())) * 100 / Integer.parseInt(yesterweek.getUserCount());
            //较上一周用户总数
            int userCountLast = (Integer.parseInt(xuekerweek.getUserZcount()) - Integer.parseInt(yesterweek.getUserZcount())) * 100 / Integer.parseInt(yesterweek.getUserZcount());
            //较上一周设备数量
            int eqCountLast = (Integer.parseInt(xuekerweek.getEqCount()) - Integer.parseInt(yesterweek.getEqCount())) * 100 / Integer.parseInt(yesterweek.getEqCount());
            //较上一周学习时长
            int zongLast = (Integer.parseInt(xuekerweek.getXueTime()) - Integer.parseInt(yesterweek.getXueTime())) * 100 / Integer.parseInt(yesterweek.getXueTime());
            //较上一周学习次数
            int xcountLast = (Integer.parseInt(xuekerweek.getXueCount()) - Integer.parseInt(yesterweek.getXueCount())) * 100 / Integer.parseInt(yesterweek.getXueCount());
            //较上一周学习完课率
            int avgCountLast = (Integer.parseInt(xuekerweek.getAvgCourse()) - Integer.parseInt(yesterweek.getAvgCourse())) * 100 / Integer.parseInt(yesterweek.getAvgCourse());
            //新增用户
            xuekernel.setAddCount((Integer.parseInt(xuekerweek.getUserCount())));
            xuekernel.setAddCountLast(addCountLast);
            //用户总数
            xuekernel.setUserCount((Integer.parseInt(xuekerweek.getUserZcount())));
            xuekernel.setUserCountLast(userCountLast);
            //设备总数
            xuekernel.setEqCount((Integer.parseInt(xuekerweek.getEqCount())));
            xuekernel.setEqCountLast(eqCountLast);
            //学习时长
            xuekernel.setZong((Integer.parseInt(xuekerweek.getXueTime())));
            xuekernel.setZongLast(zongLast);
            //学习次数
            xuekernel.setXcount((Integer.parseInt(xuekerweek.getXueCount())));
            xuekernel.setXcountLast(xcountLast);
            //平均完课率
            xuekernel.setAvgCount((Integer.parseInt(xuekerweek.getAvgCourse())));
            xuekernel.setAvgCountLast(avgCountLast);
            return ResponseResult.okResult(xuekernel);
        }

        if (xuekernelPage.getStatus() == 4){

        }

        return null;
    }



    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }

    public Date dayList(Date time, Integer i) {
        //获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String endDate = sdf.format(time); //当前日期
        //获取三十天前日期
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(time);
        theCa.add(theCa.DATE, -i); //最后一个数字30可改，30天的意思
        Date start = theCa.getTime();
        return start;
    }

}
